package com.File.A01File;

import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Test05 {
    public static void main(String[] args) {
        File file = new File("D:\\TestFile\\TestFileRead");
        TreeMap<String, Integer> stringIntegerTreeMap = statisticsFiles(file);
        System.out.println(stringIntegerTreeMap);
    }
    
    public static TreeMap<String,Integer> statisticsFiles(File file) {
        TreeMap<String,Integer> map = new TreeMap<>();
        File[] files = file.listFiles();
        if (files == null) return null;
        for (File file1 : files) {
            if (file1.isFile()) {
                String fileName = file1.getName();
                String[] names = fileName.split("\\.");
                String name = names[names.length - 1];
                if (map.containsKey(name)) {
                    int count = map.get(name) + 1;
                    map.put(name,count);
                } else {
                    map.put(name, 1);
                }
            } else {
                TreeMap<String, Integer> stringIntegerTreeMap = statisticsFiles(file1);
                Set<Map.Entry<String, Integer>> entries = stringIntegerTreeMap.entrySet();
                entries.forEach(e ->{
                    if(map.containsKey(e.getKey())){
                        Integer i = map.get(e.getKey()) + e.getValue();
                        map.put(e.getKey(),i);
                    } else {
                        map.put(e.getKey(),e.getValue());
                    }
                });
            }
        }
        return map;
    }
}
